package com.qf.dao.impl;

import com.qf.dao.DetailsDao;
import com.qf.entity.Company;
import com.qf.entity.Details;
import com.qf.util.DruidUtil;
import com.qf.util.Page;
import com.sun.org.apache.regexp.internal.REUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
杨威
2021/9/24
19:24
*/
public class DetailsDaoImpl implements DetailsDao {
    QueryRunner queryRunner=new QueryRunner();
    @Override
    public List<Details> selectAll(Page page) {
        String sql="select * from details\n" +
                "inner join company\n" +
                "on details.companyId=company.companyId limit ?,?;";
//        String sql="select id,addrName,phone,address,date,state,company.companyId,companyName from details \n" +
//                "inner join company\n" +
//                "on details.companyId=company.companyId limit ?,?;";
        try {
            List<Map<String, Object>> maps = queryRunner.query(DruidUtil.getConnection(), sql, new MapListHandler(), page.getStartRow(), page.getPageSize());
            if(maps==null){
                return null;
            }
            List<Details> list=new ArrayList();
            for (Map<String, Object> map : maps) {
                Details details=new Details();
                Company company=new Company();
                BeanUtils.populate(details,map);
                BeanUtils.populate(company,map);
                details.setCompany(company);
                list.add(details);
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public Long selectCount() {
        try {
            String sql="select count(*) from details";
            Long query = queryRunner.query(DruidUtil.getConnection(), sql, new ScalarHandler<>());
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public int insert(Details details,Integer companyId) {
        String sql="insert into details values (0,?,?,?,?,?,?)";
        Object[] params={details.getAddrName(),details.getPhone(),details.getAddress(),details.getDate(),details.getState(),companyId};
        try {
            int update = queryRunner.update(DruidUtil.getConnection(), sql, params);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public Details selectById(Integer id) {
        try {
            String sql="select * from details \n" +
                    "inner join company\n" +
                    "on details.companyId=company.companyId where id=?;";
            Map<String, Object> query = queryRunner.query(DruidUtil.getConnection(), sql, new MapHandler(), id);
            Details details=new Details();
            Company company=new Company();
            BeanUtils.populate(details,query);
            BeanUtils.populate(company,query);
            details.setCompany(company);
            return details;
//
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public int updateDetails(Details details, Integer companyId) {
        try {
            String sql="update details set addrName=?,phone=?,address=?,date=?,state=?,companyId=? where id=?";
            Object[] params={details.getAddrName(),details.getPhone(),details.getAddress(),details.getDate(),details.getState(),companyId,details.getId()};
            int i = queryRunner.update(DruidUtil.getConnection(), sql, params);
            return i;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public int delete(Integer id) {
        try {
            String sql="delete from details where id=?";
            int update = queryRunner.update(DruidUtil.getConnection(), sql, id);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
